// Package controller @author dingrui @since 2021/11/28
package controller

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/hero"

	"github.com/bannirui/mini-prize/dal/model"
	"github.com/bannirui/mini-prize/dal/service"
	"github.com/bannirui/mini-prize/web/util/response"
)

func AdminResultRouter(party iris.Party) {
	var (
		party_     = party.Party("/admin-result")
		controller = AdminResultController{}
	)
	party_.Get("/", hero.Handler(controller.Get))
	party_.Delete("/", hero.Handler(controller.GetDelete))
	party_.Get("/cheat", hero.Handler(controller.GetCheat))
	party_.Get("/reset", hero.Handler(controller.GetReset))
}

type AdminResultController struct {
}

func (c *AdminResultController) Get(ctx iris.Context) {
	giftId := ctx.URLParamIntDefault("giftId", 0)
	uid := ctx.URLParamIntDefault("uid", 0)
	page := ctx.URLParamIntDefault("page", 1)
	size := ctx.URLParamIntDefault("size", 10)
	pagePrev := -1
	pageNext := -1
	// 数据列表
	var dataList []model.Result
	if giftId > 0 {
		dataList = service.G_resultService.SearchByGift(giftId, page, size)
	} else if uid > 0 {
		dataList = service.G_resultService.SearchByUser(uid, page, size)
	} else {
		dataList = service.G_resultService.GetAll(page, size)
	}
	total := (page - 1) + len(dataList)
	// 数据总数
	if len(dataList) >= size {
		if giftId > 0 {
			total = int(service.G_resultService.CountByGift(giftId))
		} else if uid > 0 {
			total = int(service.G_resultService.CountByUser(uid))
		} else {
			total = int(service.G_resultService.CountAll())
		}
		pageNext = page + 1
	}
	if page > 1 {
		pagePrev = page - 1
	}
	response.Ok(ctx, response.CODE_OK, &response.PageResultVO{
		GiftId:   giftId,
		Uid:      uid,
		Datalist: dataList,
		Total:    total,
		PagePrev: pagePrev,
		PageNext: pageNext,
	})
	return
}

func (c *AdminResultController) GetDelete(ctx iris.Context) {
	var (
		id  int
		err error
	)
	if id, err = ctx.URLParamInt("id"); err != nil {
		goto ERR
	}
	if err = service.G_resultService.Delete(id); err != nil {
		goto ERR
	}
	response.Ok(ctx, response.CODE_OK, nil)
	return
ERR:
	response.InternalServerError(ctx, response.CODE_SYS_PARSE_PARAMS_ERROR)
	return
}

func (c *AdminResultController) GetCheat(ctx iris.Context) {
	var (
		id  int
		err error
	)
	if id, err = ctx.URLParamInt("id"); err != nil {
		goto ERR
	}
	if err = service.G_resultService.Update(&model.Result{Id: id, Status: 2}, []string{"status"}); err != nil {
		goto ERR
	}
	response.Ok(ctx, response.CODE_OK, nil)
	return
ERR:
	response.InternalServerError(ctx, response.CODE_SYS_PARSE_PARAMS_ERROR)
	return
}

func (c *AdminResultController) GetReset(ctx iris.Context) {
	var (
		id  int
		err error
	)
	if id, err = ctx.URLParamInt("id"); err != nil {
		goto ERR
	}
	if err = service.G_resultService.Update(&model.Result{Id: id, Status: 0}, []string{"status"}); err != nil {
		goto ERR
	}
	response.Ok(ctx, response.CODE_OK, nil)
	return
ERR:
	response.InternalServerError(ctx, response.CODE_SYS_PARSE_PARAMS_ERROR)
	return
}
